package com.develop.dao;

import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.LinkedList;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.jdbc.core.BatchPreparedStatementSetter;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;

import com.develop.BeanUtils.AlarmInfo;

@Component
public class AlarmDao {
	private static final Logger logger = LoggerFactory.getLogger(AlarmDao.class);
	@Autowired
	private JdbcTemplate jdbcTemplate;
	@Value("${province}")
	public String province;
	@Value("${firmName}")
	public String firmName;
	@Value("${omcType}")
	public String omcType;

	public void batchSaveAlarmInfo(LinkedList<AlarmInfo> alarmList) {
		if (alarmList.size() > 0) {
			long now = System.currentTimeMillis();
			String sql = "insert into realtimealarm(alarmSeq,alarmTitle,alarmStatus,alarmType,origSeverity,eventTime,omcReceivedTime,alarmId,"
					+ "specificProblemID,specificProblem,omcUID,neUID,neName,neType,objectUID,objectName,objectType,locationInfo,eSerialNum,addInfo,rNeUID,rNeName,rNeType,province,firmName,omcType) " + "value (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
			jdbcTemplate.batchUpdate(sql, new BatchPreparedStatementSetter() {

				@Override
				public void setValues(PreparedStatement ps, int i) throws SQLException {
					AlarmInfo alarm = alarmList.get(i);
					ps.setInt(1, alarm.getAlarmSeq());
					ps.setString(2, alarm.getAlarmTitle());
					ps.setInt(3, alarm.getAlarmStatus());
					ps.setString(4, alarm.getAlarmType());
					ps.setInt(5, alarm.getOrigSeverity());
					ps.setString(6, alarm.getEventTime());
					ps.setString(7, alarm.getOmcReceivedTime());
					ps.setString(8, alarm.getAlarmId());
					ps.setString(9, alarm.getSpecificProblemID());
					ps.setString(10, alarm.getSpecificProblem());
					ps.setString(11, alarm.getOmcUID());
					ps.setString(12, alarm.getNeUID());
					ps.setString(13, alarm.getNeName());
					ps.setString(14, alarm.getNeType());
					ps.setString(15, alarm.getObjectUID());
					ps.setString(16, alarm.getObjectName());
					ps.setString(17, alarm.getObjectType());
					ps.setString(18, alarm.getLocationInfo());
					ps.setString(19, alarm.geteSerialNum());
					ps.setString(20, alarm.getAddInfo());
					ps.setString(21, alarm.getrNeUID());
					ps.setString(22, alarm.getrNeName());
					ps.setString(23, alarm.getrNeType());
					ps.setString(24, province);
					ps.setString(25, firmName);
					ps.setString(26, omcType);
				}

				@Override
				public int getBatchSize() {
					return alarmList.size();
				}
			});
			logger.info("success-saveAlarmInfo:" + alarmList.size() + ",use-time:" + (System.currentTimeMillis() - now) + "ms");
		}
	}

	// 查询数据库中此城市厂家最大的alarmSeq
	public Integer queryMaxAlarmSeq() {
		String sql = "select max(alarmSeq) from realtimealarm where province =?  and firmName=?";
		return jdbcTemplate.queryForObject(sql, Integer.class, province, firmName);
	}
}
